package strategy;

import java.util.ArrayList;

import aliveObjs.AliveObject;
import aliveObjs.Herbivorous;

import specObjs.SpecialObject;

public class HerbiStrategy extends AbstractStrategy{
	protected Herbivorous herbiObj;
	
	public HerbiStrategy(Herbivorous herbiObj){
		this.herbiObj = herbiObj;
	}
	
	public void do_strategy(ArrayList<SpecialObject> specObjs, 
			ArrayList<AliveObject> aliveObjs){
		findFood(specObjs);
		createGood();		
	}
	
	protected void findFood( ArrayList<SpecialObject> specObjs){
		for(SpecialObject so : specObjs){
			if (so.IsFood()){
				if (isVisible(so, herbiObj)){
					moveTo(so.getX(), so.getY());
					break;
				}
			}
		}
	}
	
	protected void moveTo(int x, int y){
		if (x >= this.dx) this.x += this.dx;
		else this.x += x;
		if (y >= this.dy) this.y += this.dy;
		else this.y += y;
	}
	
	private Boolean isVisible(SpecialObject so, AliveObject ao){
		int dx = ao.getStrategy().x - so.getX();
		int dy = ao.getStrategy().y - so.getY();
		double d = Math.sqrt(dx*dx + dy*dy);
		return ( (double) ao.getStrategy().view_dist >= d); 
	}
	
	protected void createGood(){
		this.herbiObj.setGoods(10);
	}
}
